//
//  TTHeadView.m
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/27.
//  Copyright © 2016年 陶路路. All rights reserved.
//

#import "TTHeadView.h"
#import <Masonry/Masonry.h>
#import "TTHomeCellModel.h"
#import "TTHomeViewOTO.h"
@interface TTHeadView ()

@property (nonatomic,strong) UIImageView * headImageView;
@property (nonatomic,strong) UIImageView * VimageView;
@property (nonatomic,strong) UIImageView * vipImageView;
@property (nonatomic,strong) UILabel * nameLable;
@property (nonatomic,strong) UILabel * timeLabe;
@property (nonatomic,strong) UILabel * fromLable;
@property (nonatomic,strong) NSDateFormatter * formatter;
@end

@implementation TTHeadView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUpUI];
        [self setUpLayout];
        self.formatter = [[NSDateFormatter alloc]init];
    }
    return self;
}

-(void)setHeadModel:(TTHomeCellModel *)headModel{
    
    _headModel = headModel;
    
    self.nameLable.text = headModel.cellModel.user.name;
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:headModel.cellModel.user.profile_image_url]];
    
    self.VimageView.image = headModel.verified_type;
    
    self.vipImageView.image = headModel.mbrank;
    
    self.timeLabe.text = [self getTimeWith:headModel.cellModel.created_at];
    
    self.fromLable.attributedText = headModel.souce;
    
}

-(void)setUpUI{
    
    [self addSubview:self.headImageView];
    [self addSubview:self.VimageView];
    [self addSubview:self.vipImageView];
    [self addSubview:self.nameLable];
    [self addSubview:self.timeLabe];
    [self addSubview:self.fromLable];
    
}
#pragma mark 懒加载

-(UIImageView *)headImageView{
    
    if (_headImageView == nil) {
        
        _headImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_default"]];
    }
    
    return _headImageView;
}

-(UIImageView *)VimageView{
    
    if (_VimageView == nil) {
        
        _VimageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_vgirl"]];
    }
    
    return _VimageView;
}

-(UIImageView *)vipImageView{
    
    if (_vipImageView == nil) {
        
        _vipImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_icon_membership"]];
    }
    return _vipImageView;
}

-(UILabel *)nameLable{
    
    if (_nameLable == nil) {
        
        _nameLable = [[UILabel alloc]init];
        _nameLable.text = @"大强";
        _nameLable.font = [UIFont systemFontOfSize:15];
    }
    
    return _nameLable;
}

-(UILabel *)timeLabe{
    
    if (_timeLabe == nil) {
        
        _timeLabe = [[UILabel alloc]init];
        _timeLabe.text = @"刚刚";
        _timeLabe.textColor = [UIColor orangeColor];
        _timeLabe.font = [UIFont systemFontOfSize:12];
    }
    
    return _timeLabe;
}

-(UILabel *)fromLable{
    
    if (_fromLable == nil) {
        
        _fromLable = [[UILabel alloc]init];
        _fromLable.text = @"来自ios";
        _fromLable.textColor = [UIColor orangeColor];
        _fromLable.font = [UIFont systemFontOfSize:12];
    }
    return _fromLable;
}

-(void)setUpLayout{
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.equalTo(@10);
        make.width.height.equalTo(@35);
    }];
    // 02
    
    [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headImageView);
        make.leading.equalTo(self.headImageView.mas_trailing).offset(10);
    }];
    // 03
    
    [self.vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.nameLable.mas_trailing).offset(10);
        make.centerY.equalTo(self.nameLable);
    }];
    // 04
    
    [self.timeLabe mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.nameLable);
        make.bottom.equalTo(self.headImageView);
    }];
    // 05
    
    [self.fromLable mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.leading.equalTo(self.timeLabe.mas_trailing).offset(10);
        make.bottom.equalTo(self.timeLabe);
    }];
    // 06
    
    [self.VimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.headImageView.mas_trailing);
        make.centerY.equalTo(self.headImageView.mas_bottom);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.headImageView).offset(10);
    }];
    
    
}


-(NSString*)getTimeWith:(NSString*)time{
    
    self.formatter.dateFormat = @"EEE MMM dd HH:mm:ss z yyyy";
    
    self.formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    
    NSDate * timeDate = [self.formatter dateFromString:time];
    
//    NSDate * timeDate = [timeDate1 dateByAddingTimeInterval:-66666666];
    
    if ([self isthisYear:timeDate]) {
        
        NSCalendar * calender = [NSCalendar currentCalendar];
        
        if ([calender isDateInToday:timeDate]) {
            
            NSInteger intValue = [[NSDate date] timeIntervalSinceDate:timeDate];
            
            if (intValue < 60) {
                
                return @"刚刚";
                
            }else if (intValue >= 60 && intValue < 3600){
                
                return [NSString stringWithFormat:@"%ld分钟之前",intValue/60];
                
            }else if (intValue >= 3600 && intValue < 3600 * 24){
                
                return [NSString stringWithFormat:@"%ld小时之前",intValue/3600];
            }
        }else{
            
            if ([calender isDateInYesterday:timeDate]) {
                
                self.formatter.dateFormat = @"HH:mm";
                
            }else{
                
                self.formatter.dateFormat = @"MM月dd日 HH:mm";
            }
        }
        
    }else{
        
        self.formatter.dateFormat = @"yyyy年MM月dd日 HH:mm";
    }
 
    return [self.formatter stringFromDate:timeDate];
}


-(BOOL)isthisYear:(NSDate*)timeDate{
    
    self.formatter.dateFormat = @"yyyy";
    
    NSString * yearDate = [self.formatter stringFromDate:timeDate];
    
    NSString * current = [self.formatter stringFromDate:[NSDate date]];
    
    return [yearDate isEqualToString:current];
}

@end
